<template>
    <el-form label-width="100px">
        <el-form-item label="SPU名称" prop="name">
            <el-input placeholder="SPU名称" v-model="skuParams.skuName"></el-input>
        </el-form-item>
        <el-form-item label="价格(元)" prop="name">
            <el-input placeholder="价格(元)" type="number" v-model="skuParams.price"></el-input>
        </el-form-item>
        <el-form-item label="重量(g)" prop="name">
            <el-input placeholder="重量(g)" type="number" v-model="skuParams.weight"></el-input>
        </el-form-item>
        <el-form-item label="SKU描述" prop="name">
            <el-input placeholder="SKU描述" type="textarea" v-model="skuParams.skuDesc"></el-input>
        </el-form-item>
        <el-form-item label="销售属性" prop="">
            <el-form :inline="true">
                <el-form-item v-for="(item, index) in saleArr" :key="item.id" :label="item.saleAttrName" prop=""
                    style="width: 300px;">
                    <el-select v-model="item.saleIdAndValueId">
                        <el-option v-for="(saleAttrValue, index) in item.spuSaleAttrValueList" :key="saleAttrValue.id"
                            :label="saleAttrValue.saleAttrValueName"
                            :value="`${item.id}:${saleAttrValue.id}`"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
        </el-form-item>
        <el-form-item label="图片名称" prop="">
            <el-table border :data="imgArr" ref="table">
                <el-table-column type="selection" width="80px" align="center"></el-table-column>
                <el-table-column label="图片">
                    <template #="{ row, $index }">
                        <img :src="row.imgUrl" alt="" style="width: 100px; height: 100px;">
                    </template>
                </el-table-column>
                <el-table-column label="名称" prop="imgName">
                </el-table-column>
                <el-table-column label="操作">
                    <template #="{ row, $index }">
                        <el-button type="primary" size="small" @click="handler(row)">设置默认</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-form-item>
        <el-form-item label="" prop="">
            <el-button type="primary" @click="save">保存</el-button>
            <el-button type="primary" @click="cnacel">取消</el-button>
        </el-form-item>
    </el-form>
</template>

<script setup lang='ts'>
let $emit = defineEmits(['changeScene'])
import { reqAttr } from '@/api/product/attr'
import { reqBaseSpu, reqAddSku } from '@/api/product/spu'
import { ref, reactive } from 'vue'
// /平台属性
let attrArr = ref<any>([])
//销售属性
let saleArr = ref<any>([])
//照片墙数据
let imgArr = ref<any>()
//收集sku的参数
let skuParams = reactive<any>({
    //父组件传递过来的数据
    "category3Id": "",//三级分类的ID
    "spuId": "",//已有的SPU的ID
    "tmId": "",//SPU品牌的ID
    //v-model收集
    "skuName": "",//sku名字
    "price": "",//sku价格
    "weight": "",//sku重量
    "skuDesc": "",//sku的描述
    "skuAttrValueList": [//平台属性的收集
    ],
    "skuSaleAttrValueList": [//销售属性
    ],
    "skuDefaultImg": "",//sku图片地址
})
//获取图片table组件的实例
let table = ref<any>()
//取消按钮实现场景切换
const cnacel = () => {
    $emit('changeScene', { flag: 0, params: '' })
}
//当前子组件方法对外暴露
const initSkuData = async (c1Id: number | string, c2Id: number | string, spu: any) => {
    skuParams.category3Id = spu.category3Id
    skuParams.spuId = spu.id
    skuParams.tmId = spu.tmId
    //获取添加sku需要的数据在这发请求
    //获取平台属性
    // let result: any = await reqAttr(c1Id, c2Id, spu.category3Id)
    //获取对应的销售属性和照片墙
    let result: any = await reqBaseSpu(spu.id);
    imgArr.value = result.data.spuImageList
    saleArr.value = result.data.spuSaleAttrList
}
//设置默认图片的方法
const handler = (row: any) => {
    //点击的时候，全部的复选框不勾选
    imgArr.value.forEach((item: any) => {
        table.value.toggleRowSelection(item, false)
    })
    //选中的才勾上
    table.value.toggleRowSelection(row, true)
    skuParams.skuDefaultImg = row.imgUrl
}
//保存按钮的方法
const save = async () => {
    //整理参数
    skuParams.skuSaleAttrValueList = saleArr.value.reduce((prev: any, next: any) => {
        if (next.saleIdAndValueId) {
            let [saleAttrId, saleAttrValueId] = next.saleIdAndValueId.split(':');
            prev.push({
                saleAttrId, saleAttrValueId
            })
        }
        return prev;
    }, [])
    let result: any = await reqAddSku(skuParams)
}
defineExpose({ initSkuData })
</script>

<style></style>